﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sockets2;
using System.Net.Sockets;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Sockets2
{
    public class Cliente
    {
        TcpClient clienteSocket;
        NetworkStream networkStream;
        StreamReader streamReader;
        StreamWriter streamWriter;

        public bool Conectar(string ip)
        {
            //Creamos un TcpCliente y le pasamos 
            //la ip y el puerto.
            try
            {
                if (this.Conectado() == false)
                {
                    clienteSocket = new TcpClient(ip, 9898);
                    return true;
                }
                return false;
            }
            catch (Exception)
            {

                return false;
            }
      
        }
        public bool Conectado()
        {
            try
            {
                return clienteSocket.Connected;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public void Enviar(string texto)
        {
             networkStream = clienteSocket.GetStream();
             streamReader =  new System.IO.StreamReader(networkStream);
             streamWriter =  new System.IO.StreamWriter(networkStream);

            try
            {
                streamWriter.WriteLine(texto);
                streamWriter.Flush();
            }
            catch
            {
                MessageBox.Show("EXCEPTION");
            }
        }
        public bool nuevoMsj()
        {
            if (clienteSocket.Available > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public string Recibir()
        {              
                    networkStream = clienteSocket.GetStream();
                    streamReader = new System.IO.StreamReader(networkStream);
                    string outputString = streamReader.ReadLine();        
                    
                    return outputString;
        }

        public void RecibirArchivo()
        {

            byte[] clientData = new byte[1024 * 5000];

            int receivedBytesLen = clienteSocket.Client.Receive(clientData);

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            //Console.WriteLine("Client:{0} connected & File {1} started received.", clienteSocket.Client.RemoteEndPoint, fileName);

            BinaryWriter bWrite = new BinaryWriter(File.Open( "Nuevo.txt", FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            bWrite.Close();
            


            //clientSock.Send(clientData);

        }
        public void Desconectar()
        {
            try
            {
                networkStream.Close();

            }
            catch (Exception)
            {

            }

            try
            {

                streamReader.Close();

            }
            catch (Exception)
            {

            }
            try
            {

                streamWriter.Close();
            }
            catch (Exception)
            {

            }

            try
            {
                clienteSocket.Close();
            }
            catch (Exception)
            {

            }

        }

    }
}
